package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Loser
 * @Date: Created in 2020-10-15 09:14
 * @Description: 用户权限业务层实现类
 * @Path: com.itheima.service.PermissionServiceImpl
 * @Modified By:
 */

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService{

    /**
     * 用户角色
     */
    @Autowired
    private RoleDao roleDao;

    /**
     * 用户表
     */
    @Autowired
    private UserDao userDao;

    /**
     * 角色权限项
     */
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public void addPsermisson(Permission permission){
        permissionDao.add(permission);
    }

    /**
     * 添加用户权限项
     * @param role
     */
    @Override
    public void add(Role role) {
        //添加方法将role对象添加到数据库
        roleDao.save(role);
        //从role中获取到关键字id进行关联
        Integer id = role.getId();
        List<Integer> permissionIds = role.getPermissionIds();
        for (Integer permissionId : permissionIds) {
            //往中间表t_role_permission添加关联id
            Map map = new HashMap();
            map.put("id",id);
            map.put("permissionid",permissionId);
            roleDao.setRoleIdAndPermissionId(map);
        }
        //添加中间表
        List<Integer> menuIds = role.getMenuIds();
        for (Integer menuId : menuIds) {
            //菜单与角色的关联表
            //存为map集合进行存储
            Map map = new HashMap();
            map.put("id",id);
            map.put("menuId",menuId);
            roleDao.setMenuIdAndRoleId(map);
        }
    }

    /**
     * 根据id查询用户权限项
     * @param permissionId
     * @return
     */
    @Override
    public String findByIdFormName(Integer permissionId) {
        String permissionDaoByIdFormName = permissionDao.findByIdFormName(permissionId);
        return permissionDaoByIdFormName;
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Permission> page = permissionDao.findPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 修改权限
     * @param permission
     */
    @Override
    public void edit(Permission permission) {
        permissionDao.update(permission);
    }

    /**
     * 根据id查询，数据回显
     * @param id
     * @return
     */
    @Override
    public Permission findById(Integer id) {
        return permissionDao.findByPermissionId(id);
    }

    /**
     * 删除数据
     * @param id
     */
    @Override
    public void delete(Integer id) {
        permissionDao.delete(id);
    }
}
